package com.etop.weibo.service;

import com.etop.utils.MD5;
import com.etop.weibo.annotation.Daodi;
import com.etop.weibo.beans.Comment;
import com.etop.weibo.beans.Praise;
import com.etop.weibo.beans.Say;
import com.etop.weibo.beans.Secret;
import com.etop.weibo.beans.StarAndFan;
import com.etop.weibo.beans.User;
import com.etop.weibo.dao.DAOFactory;
import com.etop.weibo.dao.SecretDao;
import com.etop.weibo.dao.UserDao;
import com.etop.weibo.dto.UserCommentDTO;
import com.etop.weibo.dto.UserSayDTO;
import com.etop.weibo.util.BeansUtil;
import com.etop.weibo.util.WeiBoException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jdk.nashorn.internal.objects.NativeString;

/**
 *
 * @author jansing lz_jansing@163.com
 */
public class UserService extends BaseService{

    
    private UserDao userDao = DAOFactory.getUserDao();
    private SecretDao secretDao = DAOFactory.getSecretDao();

    public UserDao getUserDao() {
        return userDao;
    }

    @Daodi
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public SecretDao getSecretDao() {
        return secretDao;
    }

    @Daodi
    public void setSecretDao(SecretDao secretDao) {
        this.secretDao = secretDao;
    }
    
    public void login(HttpServletRequest req, HttpServletResponse resp) {
        User u = (User) BeansUtil.createBean(User.class, req.getParameterMap());
        u.setPassword(new MD5().getkeyBeanofStr(u.getPassword()));
        Map<String, Object> condition = new HashMap<>();
        condition.put(UserDao.USERNAME, u.getUsername());
        User user = userDao.loadByHQL(condition);
        if (user == null) {
            throw new WeiBoException("账号不存在");
        }
        if (user.getPassword().equals(u.getPassword())) {
            req.getSession().setAttribute("user", user);
        } else {
            throw new WeiBoException("密码错误");
        }
    }

    public void logout(HttpServletRequest req, HttpServletResponse resp) {
        req.getSession().invalidate();
    }

    public void sign(HttpServletRequest req, HttpServletResponse resp) {
        User u = (User) BeansUtil.createBean(User.class, req.getParameterMap());
        if(u.getUsername()==null || u.getUsername().trim().equals("") 
                || u.getPassword()==null || u.getPassword().trim().equals("")){
            throw new WeiBoException("用户名密码不能为空");
        }
        u.setPassword(new MD5().getkeyBeanofStr(u.getPassword()));
        try {
            DAOFactory.getUserDao().add(u);
        } catch (Exception e) {
            throw new WeiBoException("账号已存在");
        }
    }

    public void updatePassword(HttpServletRequest req, HttpServletResponse resp){
        User user = (User) req.getSession().getAttribute("user");
        String password = new MD5().getkeyBeanofStr(req.getParameter("password"));
        if(password==null || password.trim().equals("")){
            throw new WeiBoException("密码不能为空");
        }
        if(password.equals(user.getPassword())){
            throw new WeiBoException("密码未更改");
        }
        user.setPassword(password);
        userDao.update(user);
    }
    
    
    public void showSecret(HttpServletRequest req, HttpServletResponse resp) {
        User user = (User) req.getSession().getAttribute("user");
        List<User> userList = DAOFactory.getSecretDao().userList(user.getId());
        int withWhoseId;
        try {
            withWhoseId = Integer.parseInt(req.getParameter("withWhoseId"));
        } catch (NumberFormatException e) {
            if (userList.isEmpty()) {
                withWhoseId = 0;
            } else {
                withWhoseId = userList.get(0).getId();
            }
        }
        Map<String, Object> params = new HashMap<>();
        params.put(secretDao.IS_NEW, "true");
        Map<String, Object> condition = new HashMap<>();
        condition.put(secretDao.TO_ID, user.getId());
        condition.put(secretDao.FROM_ID, withWhoseId);
        secretDao.updateByHQL(params, condition);
        List<Secret> secretList = DAOFactory.getSecretDao().secretList(user.getId(), withWhoseId);
        req.setAttribute("userList", userList);
        req.setAttribute("secretList", secretList);
        req.setAttribute("withWhoseId", withWhoseId);
    }

    public void sendSecret(HttpServletRequest req, HttpServletResponse resp) {
        int withWhoseId = Integer.parseInt(req.getParameter("withWhoseId"));
        User user = (User) req.getSession().getAttribute("user");
        Secret s = new Secret(user.getId(), withWhoseId, req.getParameter("what"), new Date(), true);
        DAOFactory.getSecretDao().add(s);
    }

    public void showMayStarList(HttpServletRequest req, HttpServletResponse resp) {
        User user = (User) req.getSession().getAttribute("user");
        List<User> list = DAOFactory.getUserDao().list();
        list.remove(user);
        List<User> starList = DAOFactory.getStarAndFanDao().starsList(user.getId());
        for (User u : starList) {
            list.remove(u);
        }
        req.setAttribute("mayStarList", list);
    }

    public void star(HttpServletRequest req, HttpServletResponse resp) {
        User user = (User) req.getSession().getAttribute("user");
        int starId = Integer.parseInt(req.getParameter("starId"));
        StarAndFan sf = new StarAndFan(starId, user.getId());
        DAOFactory.getStarAndFanDao().add(sf);
    }

    public void unStar(HttpServletRequest req, HttpServletResponse resp) {
        User user = (User) req.getSession().getAttribute("user");
        int starId = Integer.parseInt(req.getParameter("starId"));
        DAOFactory.getStarAndFanDao().delete(starId, user.getId());
    }

    public void showStarsList(HttpServletRequest req, HttpServletResponse resp) {
        User user = (User) req.getSession().getAttribute("user");
        List<User> list = DAOFactory.getStarAndFanDao().starsList(user.getId());
        req.setAttribute("starsList", list);
    }

    public void showFansList(HttpServletRequest req, HttpServletResponse resp) {
        User user = (User) req.getSession().getAttribute("user");
        List<User> list = DAOFactory.getStarAndFanDao().fansList(user.getId());
        req.setAttribute("fansList", list);
    }

    public void publish(HttpServletRequest req, HttpServletResponse resp) {
        User user = (User) req.getSession().getAttribute("user");
        Say say = new Say(user.getId(), req.getParameter("publish-input"), 0, new Date());
        DAOFactory.getSayDao().add(say);
    }

    public void deleteWeibo(HttpServletRequest req, HttpServletResponse resp) {
        User user = (User) req.getSession().getAttribute("user");
        Say say = DAOFactory.getSayDao().load(Integer.parseInt(req.getParameter("sayId")));
        if (user.getId() == say.getUserId()) {
            DAOFactory.getCommentDao().deleteAll(say.getId());
            DAOFactory.getSayDao().delete(say.getId());
        } else {
            throw new WeiBoException("非法操作");
        }
    }

    public void intoWeibo(HttpServletRequest req, HttpServletResponse resp) {
        User user = (User) req.getSession().getAttribute("user");
        int sayId = Integer.parseInt(req.getParameter("sayId"));
        Say say = DAOFactory.getSayDao().load(sayId);
        List<UserCommentDTO> commentList = DAOFactory.getCommentDao().list(sayId);
        req.setAttribute("say", say);
        req.setAttribute("commentList", commentList);
    }

    public void praise(HttpServletRequest req, HttpServletResponse resp) {
        User user = (User) req.getSession().getAttribute("user");
        int commentId;
        int sayId;
        try {
            sayId = Integer.parseInt(req.getParameter("sayId"));
            commentId = 0;
        } catch (NumberFormatException e) {
            sayId = 0;
            commentId = Integer.parseInt(req.getParameter("commentId"));
        }
        if (DAOFactory.getPraiseDao().load(user.getId(), sayId, commentId) == null) {
            if (sayId == 0) {
                DAOFactory.getCommentDao().praise(commentId);
            } else {
                DAOFactory.getSayDao().praise(sayId);
            }
            Praise p = new Praise(user.getId(), sayId, commentId);
            DAOFactory.getPraiseDao().add(p);
        } else {
            throw new WeiBoException("已赞");
        }
    }

    public void unPraise(HttpServletRequest req, HttpServletResponse resp) {
        User user = (User) req.getSession().getAttribute("user");
        int commentId;
        int sayId;
        try {
            sayId = Integer.parseInt(req.getParameter("sayId"));
            commentId = 0;
        } catch (NumberFormatException e) {
            sayId = 0;
            commentId = Integer.parseInt(req.getParameter("commentId"));
        }
        if (sayId == 0) {
            DAOFactory.getCommentDao().unPraise(commentId);
        } else {
            DAOFactory.getSayDao().unPraise(sayId);
        }
        DAOFactory.getPraiseDao().delete(user.getId(), sayId, commentId);
    }

    public void comment(HttpServletRequest req, HttpServletResponse resp) {
        User user = (User) req.getSession().getAttribute("user");
        int sayId = Integer.parseInt(req.getParameter("sayId"));
        Date createDate = new Date();
        int commentId;
        Date listDate;
        try {
            commentId = Integer.parseInt(req.getParameter("commentId"));
            listDate = DAOFactory.getCommentDao().load(commentId).getListTime();
        } catch (NumberFormatException e) {
            commentId = 0;
            listDate = createDate;
        }
        String what = req.getParameter("what");
        Comment comment = new Comment(user.getId(), sayId, commentId, what, 0, createDate, listDate);
        DAOFactory.getCommentDao().add(comment);
    }

    public void deleteComment(HttpServletRequest req, HttpServletResponse resp) {
        User user = (User) req.getSession().getAttribute("user");
        Comment comment = DAOFactory.getCommentDao().load(Integer.parseInt(req.getParameter("commentId")));
        if (user.getId() == comment.getUserId()) {
            DAOFactory.getCommentDao().delete(comment.getId());
        } else {
            throw new WeiBoException("非法操作");
        }
    }

    public void indexPage(HttpServletRequest req, HttpServletResponse resp) {
//        List<UserSayDTO> usList = DAOFactory.getSayDao().list();
//        req.setAttribute("usList", usList);
    }

    public void personalCenter(HttpServletRequest req, HttpServletResponse resp) {
        User host = DAOFactory.getUserDao().load(Integer.parseInt(req.getParameter("hostId")));
        List<UserSayDTO> sayList = DAOFactory.getSayDao().list(host.getId());
        req.setAttribute("host", host);
        req.setAttribute("sayList", sayList);
    }

}
